<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-12-17
 * Time: 17:22
 */

namespace app\index\controller;


use app\common\controller\HomeBase;
use app\common\model\File;
use app\common\model\Projectm;
use app\common\model\Roofplacementm;
use app\common\model\User;
use think\Db;

class Project extends HomeBase
{

    /**
     * 前端资金盘页面
     */
    public function capitaldisk()
    {
        $params = input();
        if(array_key_exists("u_id",$params) && $params['u_id'] != ''){
            $list['info'] = User::userSecuritiesIncome($params['u_id']);
            //昨日总收益
            $list['yesterday']  = Db::name('user_securities')->where('us_uid', $params['u_id'])->sum('us_yesterday_earnings');
            //所有证券的市值总和
            $list['allmoney']  = Db::name('user_securities')->where('us_uid', $params['u_id'])->sum('us_securities_money');
            if (!$list['yesterday']) {
                $list['yesterday'] = '0.00';
            }
            if (!$list['allmoney']) {
                $list['allmoney'] = '0.00';
            }
            //累计总收益
            $list['all_income'] = Db::name('user')->where('u_id', $params['u_id'])->value('u_all_income');
            return json(['code' => 200, 'msg' => $list]);
        } else {
            return json(['code' => 300, 'msg' => '未登录']);
        }
    }

    /**
     * 抢购证券
     */
    public function panicBuyingSecurities()
    {
        $params = input();
        $validate_result = $this->validate($params, 'Wproject');
        if ($validate_result !== true) {
            return  json(['code' => 300, 'msg' => $validate_result]);
        }
        $res = Projectm::userBuyProject($params['p_id'], $params['u_id']);
        if ($res === -1) {
            return json(['code' => 300, 'msg' => '账户余额不足，抢购失败。']);
        }
        if ($res === -2) {
            return json(['code' => 300, 'msg' => '该证券已售空！']);
        }
        if (!$res) {
            return json(['code' => 300, 'msg' => '抢购失败，请稍后再试']);
        }
        return json(['code' => 200, 'msg' => '恭喜您抢购一张'.$res.'成功']);
    }

    /**
     * 入金
     */
    public function goldentry()
    {
        $params = input();
        $validate_result = $this->validate($params, 'Entry');
        if ($validate_result !== true) {
            return  json(['code' => 300, 'msg' => $validate_result]);
        }
        if(!is_numeric($params['money'])||strpos($params['money'],".")!==false){
            return  json(['code' => 300, 'msg' => '入金额度必须为整数']);
        }
        $res = Projectm::userEnterEntry($params);
        if ($res === -1) {
            return  json(['code' => 300, 'msg' => '账户余额不足']);
        }
        if (!$res) {
            return  json(['code' => 300, 'msg' => '操作失败，请稍后重试。']);
        }
        return  json(['code' => 200, 'msg' => '入金成功']);
    }
}